package com.example.demo.service;

import com.example.demo.app.AppUtils;
import com.example.demo.bean.Menus;
import com.example.demo.tools.MyComparator;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 数据处理
 */
@Service
public class DataProcess {

    public Object processOrder(){
        List<Menus> list = new ArrayList<>();
        list.addAll(AppUtils.menusList);

        MyComparator c = new MyComparator();
        //list 排序
        list.sort(c);
        Menus root = new Menus();
        for (Menus menu : list) {
            if(menu.getUp_perm_id() == 0){
                root = menu;
                break;
            }
        }
        List<Menus> rootList = getMenus(list, root);
        root.setList(rootList);

        return root;
    }

    protected List<Menus> getMenus(List<Menus> list,Menus parent){
        List<Menus> tempList = new ArrayList<>();
        //查出更目录
        for (Menus menu : list) {
            if(menu.getUp_perm_id() == parent.getPerm_id()){
                tempList.add(menu);
            }
        }

        MyComparator c = new MyComparator();
        tempList.sort(c);
        parent.setList(tempList);

        for (Menus child : tempList) {
            getMenus(list,child);
        }

        return tempList;
    }
}
